var database = require('../../../data/data.js');
Page({
  data: {

  },
  onLoad: function (options) {
    var that = this;
    if (database.login == false) {
      wx.redirectTo({
        url: '/pages/newBinding/newBinding',
      })
    } else {
      wx.request({
        url: database.url + '/project/getMyMessage.action',
        data: {
          openid: database.userId
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success(res) {
          var yuezhanamessage = res.data.yuezhanamessage;
          var cancelamessage = res.data.cancelamessage;
          var nmessage = res.data.nmessage;
          // var rewardmessage = res.data.rewardmessage;
          that.setData({
            msg: yuezhanamessage.length + cancelamessage.length + nmessage.length,
            //  + rewardmessage.length,
            yuezhanamessage: yuezhanamessage,
            cancelamessage: cancelamessage,
            nmessage: nmessage,
            // rewardmessage: rewardmessage
          });
        }
      })
    }
  },
  toGamedetail: function (e) {
    var gameid = e.currentTarget.dataset.gameid;
    var popenid = e.currentTarget.dataset.popenid;
    if (e.currentTarget.dataset.msg.indexOf("同意了你的取消约战") > 0) {
      wx.redirectTo({
        url: '/pages/fight_detail/battle/battle?gameid=' + gameid + ' &popenid=' + popenid,
      })
    } else {
      wx.redirectTo({
        url: '/pages/fight_detail/publisher/publisher?gameid=' + gameid + '&popenid=' + popenid,
      })
    }
  },
  answerCancel: function (e) {
    var that = this;
    var content = '';
    if (e.currentTarget.dataset.answer == "y") {
      content = "是否同意取消";
    } else if (e.currentTarget.dataset.answer == "n") {
      content = "是否拒绝取消";
    }
    wx.showModal({
      title: '',
      content: content,
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: database.url + '/project/uploadAnswerCancelYuezhan.action',
            data: {
              amessageid: e.currentTarget.dataset.amessageid,
              gameid: e.currentTarget.dataset.gameid,
              answer: e.currentTarget.dataset.answer
            },
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success(res) {
              var cancelamessage = that.data.cancelamessage;
              for (var i = cancelamessage.length - 1; i >= 0; i--) {
                if (e.currentTarget.dataset.amessageid == cancelamessage[i].id) {
                  cancelamessage.splice(i, 1);
                }
              }
              wx.redirectTo({
                url: '/pages/my/message/message?yuezhanamessage=' + JSON.stringify(that.data.yuezhanamessage) + '&cancelamessage=' + JSON.stringify(that.data.cancelamessage) + '&nmessage=' + JSON.stringify(that.data.nmessage) + '&rewardmessage=' + JSON.stringify(that.data.rewardmessage),
              })
            }
          });
        }
      }
    });
  },
  deleteyuezhanamessage: function (e) {
    var that = this;
    wx.showModal({
      title: '',
      content: '是否删除消息',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: database.url + '/project/uploadAmessage.action',
            data: {
              amessageid: e.currentTarget.dataset.amessageid,
            },
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success(res) {
              var yuezhanamessage = that.data.yuezhanamessage;
              for (var i = yuezhanamessage.length - 1; i >= 0; i--) {
                if (e.currentTarget.dataset.amessageid == yuezhanamessage[i].id) {
                  yuezhanamessage.splice(i, 1);
                }
              }
              wx.redirectTo({
                url: '/pages/my/message/message?yuezhanamessage=' + JSON.stringify(that.data.yuezhanamessage) + '&cancelamessage=' + JSON.stringify(that.data.cancelamessage) + '&nmessage=' + JSON.stringify(that.data.nmessage) + '&rewardmessage=' + JSON.stringify(that.data.rewardmessage),
              })
            }
          });
        }
      }
    });
  },
  deletecancelamessage: function (e) {
    var that = this;
    wx.showModal({
      title: '',
      content: '是否删除消息',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: database.url + '/project/uploadAmessage.action',
            data: {
              amessageid: e.currentTarget.dataset.amessageid,
            },
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success(res) {
              var cancelamessage = that.data.cancelamessage;
              for (var i = cancelamessage.length - 1; i >= 0; i--) {
                if (e.currentTarget.dataset.amessageid == cancelamessage[i].id) {
                  cancelamessage.splice(i, 1);
                }
              }
              wx.redirectTo({
                url: '/pages/my/message/message?yuezhanamessage=' + JSON.stringify(that.data.yuezhanamessage) + '&cancelamessage=' + JSON.stringify(that.data.cancelamessage) + '&nmessage=' + JSON.stringify(that.data.nmessage) + '&rewardmessage=' + JSON.stringify(that.data.rewardmessage),
              })
            }
          });
        }
      }
    });
  },
  deletenmessage: function (e) {
    var that = this;
    wx.showModal({
      title: '',
      content: '是否删除消息',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: database.url + '/project/uploadDeleteNmessage.action',
            data: {
              nmessageid: e.currentTarget.dataset.nmessageid,
            },
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success(res) {
              var nmessage = that.data.nmessage;
              for (var i = nmessage.length - 1; i >= 0; i--) {
                if (e.currentTarget.dataset.nmessageid == nmessage[i].id) {
                  nmessage.splice(i, 1);
                }
              }
              wx.redirectTo({
                url: '/pages/my/message/message?yuezhanamessage=' + JSON.stringify(that.data.yuezhanamessage) + '&cancelamessage=' + JSON.stringify(that.data.cancelamessage) + '&nmessage=' + JSON.stringify(that.data.nmessage) + '&rewardmessage=' + JSON.stringify(that.data.rewardmessage),
              })
            }
          });
        }
      },
    })
  },
  // deleterewardmessage: function (e) {
  //   var that = this;
  //   wx.showModal({
  //     title: '',
  //     content: '是否删除消息',
  //     success: function (res) {
  //       if (res.confirm) {
  //         wx.request({
  //           url: database.url + '/project/uploadDeleteRewardmessage.action',
  //           data: {
  //             rewardmessageid: e.currentTarget.dataset.rewardmessageid,
  //           },
  //           method: "POST",
  //           header: {
  //             'content-type': 'application/x-www-form-urlencoded'
  //           },
  //           success(res) {
  //             var rewardmessage = that.data.rewardmessage;
  //             for (var i = rewardmessage.length - 1; i >= 0; i--) {
  //               if (e.currentTarget.dataset.rewardmessage == rewardmessage[i].id) {
  //                 rewardmessage.splice(i, 1);
  //               }
  //             }
  //             wx.redirectTo({
  //               url: '/pages/my/message/message?yuezhanamessage=' + JSON.stringify(that.data.yuezhanamessage) + '&cancelamessage=' + JSON.stringify(that.data.cancelamessage) + '&nmessage=' + JSON.stringify(that.data.nmessage) + '&rewardmessage=' + JSON.stringify(that.data.rewardmessage),
  //             })
  //           }
  //         });
  //       }
  //     },
  //   })
  // },
})